/*
 * Created on 02.02.2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package ro.qi.framework.sys.repository;

import java.util.StringTokenizer;
import java.util.Vector;

/**
 * @author vlad lepadatu 
 */
public class ActionConfiguration {

	private String name;
	private String template;
	private String type;
	private String content;

	StringBuffer bufferToString;

	public Vector parseAction() {
		Vector result = new Vector();
		StringTokenizer strtok = new StringTokenizer(content, "[]");
		while( strtok.hasMoreTokens() ) {
			String token = strtok.nextToken();
			if( token.length() > 0 ) {
				StringTokenizer strtok2 = new StringTokenizer(token, ";");
				ActionElement actionElement = new ActionElement();
				if( strtok2.countTokens() == 2 || token.indexOf("type:") != -1 ) {
					while( strtok2.hasMoreTokens() ) {
						String token2 = strtok2.nextToken();
						if( token2.indexOf("in:") == 0 ) {
							actionElement.setInput(true);
							actionElement.setComponentName(token2.substring(3));
						} else if( token2.indexOf("out:") == 0 ) {
							actionElement.setInput(false);
							actionElement.setComponentName(token2.substring(4));
						} else if( token2.indexOf("type:") == 0 ) {
							actionElement.setType(token2.substring(5));
						} else if( token2.indexOf("extra:") == 0 ) {
							actionElement.setExtra(token2.substring(6));
						}
					}
				} else {
					actionElement.setType(ActionElement.TYPE_NONE);
					actionElement.setComponentName(token);
				}
				result.add(actionElement);
			}
		}
		return result;
	}

	/**
	 * @return
	 */
	public String getContent() {
		return content;
	}

	/**
	 * @return
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return
	 */
	public String getType() {
		return type;
	}

	/**
	 * @param string
	 */
	public void setContent(String string) {
		content = string;
	}

	/**
	 * @param string
	 */
	public void setName(String string) {
		name = string;
	}

	/**
	 * @param string
	 */
	public void setType(String string) {
		type = string;
	}

	/**
	 * @return
	 */
	public String getTemplate() {
		return template;
	}

	/**
	 * @param string
	 */
	public void setTemplate(String string) {
		template = string;
	}

	public boolean hasTemplate() {
		return (template != null && template.length() > 0)?true:false;
	}

	public boolean matchTemplate(String templateToMatch) {
		//Logger.debug(this, "matching " + template + " with " + templateToMatch);
		// if I do not have a template on this action, then I allow it to be executed
		if(template == null || template.length() == 0)
			return true;
		// If I do not have a template on the datasource, then I do not executed it 
		if(templateToMatch == null || templateToMatch.length() == 0)
			return false;
		return (template.equalsIgnoreCase(templateToMatch))?true:false;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		if( bufferToString == null ) {
			bufferToString = new StringBuffer();
			bufferToString.append("[name=\"");
			bufferToString.append(name);
			if( hasTemplate() ) {
				bufferToString.append("\";template=\"");
				bufferToString.append(template);
			}
			bufferToString.append("\";type=\"");
			bufferToString.append(type);
			bufferToString.append("\";content=\"");
			bufferToString.append(content);
			bufferToString.append("]");
		}
		return bufferToString.toString();
	}

}
